
import 'package:example/page/button_demo.dart';
import 'package:example/page/click_demo.dart';
import 'package:example/page/dialog_demo.dart';
import 'package:example/page/expansion_tile_demo.dart';
import 'package:example/page/list_item_demo.dart';
import 'package:example/page/refresher_demo.dart';
import 'package:example/page/scroll_page_demo.dart';
import 'package:example/page/swipe_demo.dart';
import 'package:example/page/tab_menu_demo.dart';
import 'package:example/page/textField_demo.dart';
import 'package:example/page/text_demo.dart';
import 'package:example/page/web_view_demo.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_common/flutter_common.dart';
import 'package:flutter_common/ui/list/single_list_item.dart';
import 'package:flutter_common/util/context_extension.dart';
import 'package:flutter_common/util/file_extension.dart';
import 'package:flutter_common/util/log/log_printer.dart';
import 'package:flutter_common/util/log/pretty_printer.dart';
import 'package:flutter_common/util/logger.dart';
import 'package:flutter_common/util/toast_util.dart';

import 'package:flutter_ume/flutter_ume.dart'; // UME framework
import 'package:flutter_ume_kit_ui/flutter_ume_kit_ui.dart'; // UI kits
import 'package:flutter_ume_kit_perf/flutter_ume_kit_perf.dart'; // Performance kits
import 'package:flutter_ume_kit_show_code/flutter_ume_kit_show_code.dart'; // Show Code
import 'package:flutter_ume_kit_device/flutter_ume_kit_device.dart'; // Device info
import 'package:flutter_ume_kit_console/flutter_ume_kit_console.dart'; // Show debugPrint
import 'package:flutter_ume_kit_dio/flutter_ume_kit_dio.dart'; // Dio Inspector

void main() {
  // runZoned(() => runApp(MyApp()),
  //     zoneSpecification: ZoneSpecification(
  //         print: (Zone self, ZoneDelegate parent, Zone zone, String line) {
  //           parent.print(zone, "jacky print: $line");
  //         }));
  if (kDebugMode) {
    PluginManager.instance                                 // Register plugin kits
      ..register(const WidgetInfoInspector())
      ..register(const WidgetDetailInspector())
      ..register(const ColorSucker())
      ..register(AlignRuler())
      ..register(const ColorPicker())                            // New feature
      ..register(const TouchIndicator())                         // New feature
      ..register(Performance())
      ..register(const ShowCode())
      ..register(const MemoryInfoPage())
      ..register(CpuInfoPage())
      ..register(const DeviceInfoPanel())
      ..register(Console())
      // ..register(DioInspector(dio: dio))
    ;                  // Pass in your Dio instance
    // After flutter_ume 0.3.0
    runApp(UMEWidget(enable: true, child: MyApp()));
  } else {
    runApp(MyApp());
  }
}

class MyApp extends FxMaterialApp {

  LoggerFactory createLoggerFactory(bool debug, bool bridget) {
    return DefaultLoggerFactory(
        printer: PrettyPrinter(
          endMethodCount: 4,
          startMethodCount: 2,
          showLogName: true,
          // 保证通过原生到flutter 时，logcat不显示颜色
          showLevelName: bridget,
          colors: !bridget,
          // 强制不显示颜色
          // showLevelName: true,
          // colors:false,
          printEmojis: false,
        ),
        output: ConsoleLogOutput());
  }

  @override
  void initState() {
    if(SafePlatform.isAndroid()) {
      SystemChrome.setSystemUIOverlayStyle(FxTheme().darkUiStyleWhiteNavigationBar);
    }
    loggerFactory = createLoggerFactory(true, false);
    logger.debug("message");
    logger.warn("message");
    logger.info("message");
    logger.error("message");
  }

  @override
  MaterialApp buildMaterialApp(GlobalKey<NavigatorState> navigatorKey) {
    return MaterialApp(
      navigatorKey: navigatorKey,
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.green,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }

  @override
  void handlerExceptionMessage(String msg) {
    ToastUtil.errorToast(msg.substring(0, 30));
  }
}

class MyHomePage extends StatefulWidget {

  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<StatefulWidget> createState() => HomeState();

}

class HomeState extends State<MyHomePage> {

  List<Widget?> pages = [
    TextDemo(),
    ButtonDemo(),
    DialogDemo(),
    RefresherDemo(),
    SwipeDemo(),
    ScrollPageDemo(),
    TextFieldDemo(),
    TabMenuDemo(),
    ListItemDemo(),
    WebViewDemo(),
    ClickDemo(),
    ExpansionTileDemo(),
    null
  ];

  @override
  Widget build(BuildContext context) {
    FileExtension.formatFileSize();
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
        systemOverlayStyle: FxTheme().darkUiStyleWhiteNavigationBar,
      ),
      body: ListView.separated(
        itemBuilder: (BuildContext context, int index) {
          Widget? page = pages[index];
          return SingleListItem(
            page == null ? "Exception catch" : page.toStringShort(),
            onTap: () {
              logger.debug("launch page: ${page!.runtimeType}");
              if(page is DialogDemo) {
                Navigator.push(context, RotationPageRoute(page));
              } else if(page is RefresherDemo) {
                Navigator.push(context, FadePageRoute(page));
              } else {
                context.push(page!);
              }
            },
          );
        },
        separatorBuilder: (BuildContext context, int index) => FxTheme().line,
        itemCount: pages.length,
      ),
    );
  }
}

// class MyApp2 extends StatefulWidget {
//
//   @override
//   State<StatefulWidget> createState() {
//     return MyAppState();
//   }
// }
//
// class MyAppState extends State<StatefulWidget> {
//
//   final GlobalKey<NavigatorState> navigatorKey = GlobalKey();
//
//   @override
//   void initState() {
//     super.initState();
//
//     initErrorReport(navigatorKey, (msg){
//       ToastUtil.errorToast(msg.substring(0, 30));
//     });
//   }
//
//   // This widget is the root of your application.
//   @override
//   Widget build(BuildContext context) {
//     return MaterialApp(
//       navigatorKey: navigatorKey,
//       title: 'Flutter Demo',
//       theme: ThemeData(
//         primarySwatch: Colors.blue,
//       ),
//       home: const MyHomePage(title: 'Flutter Demo Home Page'),
//     );
//   }
// }